package xing.design.pattern.behavioral.Memento.sample2;

/**
 * @author xing
 * @date 2023/2/10 13:52:59
 */
public class BaseMemento implements Memento{
    /**
     * 不作为内部类，无法直接获取其属性，需要得到该对象，
     */
    private BaseOriginator originator;
    private final String info;

    public BaseMemento(BaseOriginator originator, String info) {
        this.originator = originator;
        this.info = info;
    }

    @Override
    public void restore() {
        originator.setInfo(this.info);
    }

    /**
     * 当然，也可以直接在 restore 的时候作为参数传入，自己本身不持有该 originator
     */
    @Override
    public boolean restore(Originator originator) {
        if(originator instanceof BaseOriginator) {
            ((BaseOriginator)originator).setInfo(this.info);
            return true;
        }
        return false;
    }
}
